#include<bits/stdc++.h>
using namespace std;
#define MAX 1000

struct person
{
    string name;
    int age;
    string phone;
    int gender;
    string address;
};

struct addressbooks
{
    struct person personarray[MAX];
    int size;
};

void showmedia()
{
    cout<<"================================="<<endl;
    cout<<"==========1.添加联系人============"<<endl;
    cout<<"==========2.显示联系人============"<<endl;
    cout<<"==========3.删除联系人============"<<endl;
    cout<<"==========4.查找联系人============"<<endl;
    cout<<"==========5.修改联系人============"<<endl;
    cout<<"==========6.清空联系人============"<<endl;
}

void addperson(addressbooks*abs)
{
    if(abs->size==MAX)
    {
        cout<<"联系人已满，无法添加"<<endl;
    }
    else
    {
        string name;
        cout<<"请输入姓名"<<endl;
        cin>>name;
        abs->personarray[abs->size].name=name;
        int age;
        cout<<"请输入年龄"<<endl;
        cin>>age;
        abs->personarray[abs->size].age=age;
        string phone;
        cout<<"请输入手机号"<<endl;
        cin>>phone;
        abs->personarray[abs->size].phone=phone;
        while(true)
        {
            int gender;
            cout<<"请输入您的性别"<<endl;
            cout<<"====1.男===="<<endl;
            cout<<"====2.女===="<<endl;
            cin>>gender;
            if(gender==1||gender==2)
            {
                abs->personarray[abs->size].gender=gender;
                break;
            }
            else
            {
                cout<<"输入错误请重新输入"<<endl;
            }   
        }
        string address;
        cout<<"请输入联系地址"<<endl;
        cin>>address;
        abs->personarray[abs->size].address=address;
    }
    abs->size++;
    system("psuse");
    system("cls");
}

void showperson(addressbooks*abs)
{
    if(abs->size==0)
    {
        cout<<"联系人为空"<<endl;
    }
    else
    {
        for(int i = 0;i<abs->size;i++)
        {
            cout<<"姓名: "<<abs->personarray[i].name<<"\t";
            cout<<"性别: "<<(abs->personarray[i].gender==1?"男":"女")<<"\t";
            cout<<"年龄: "<<abs->personarray[i].age<<"\t";
            cout<<"电话: "<<abs->personarray[i].phone<<"\t";
            cout<<"住址: "<<abs->personarray[i].address<<endl;
        }
    }
}

int existperson(addressbooks * abs,string name)
{
    for(int i = 0; i< abs->size;i++)
    {
        if(abs->personarray[i].name==name)
        {
            return i;//存在返回联系人姓名的i
        }
    }
    return -1;//如果遍历结束都没有找到，返回-1
}
void deleteperson(addressbooks*abs)
{
    cout<<"请输入你要删除的姓名"<<endl;
    string name;
    cin>>name;
    int ret=existperson(abs,name);
    if(ret!=-1)
    {
        for(int i=ret;i<abs->size;i++)
        {
            abs->personarray[i]=abs->personarray[i+1];
        }
        abs->size--;
        cout<<"删除成功"<<endl;
    }

}

int main()
{
    addressbooks abs;
    abs.size=0;
    int select=0;
    showmedia();
    while(true)
    {
        cin>>select;
        switch(select)
        {
            case 1:
                addperson(&abs);
                break;
            case 2:
                showperson(&abs);
                break;
            case 3:
                deleteperson(&abs);
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
        }
    }
}